#ifndef CURVE_H
#define CURVE_H

#include <QWidget>
#include "qcustomplot.h"
#include "plotpopupmenu.h"
#include "updatetimedialog.h"
#include "rangedialog.h"
#include "savecurvedialog.h"
#include "savecurvedata.h"

struct PlotXYParameter
{
    int     num; // 第几条曲线
    double  x;
    double  y;
};

class Curve : public QWidget
{
    Q_OBJECT
public:
    Curve(QWidget *parent = nullptr);
    ~Curve();

    QString getGraphName(int index);
    QCustomPlot &getQCustomPlot();
    void setXAxisName(const QString &name);
    void setYAxisName(const QString &name);
    void setTitle(const QString &title);
    QString getTitle();
    quint8 addGraph(const QString &name, const QColor &color);
    void setScopeFollow(bool en);
    void setScopeCurveUpdate(bool en);

    void setRange(double xMin, double xMax, double yMin, double yMax);
    bool eventFilter(QObject *obj, QEvent *event);

    void noise();
    void shift();
    void save();
    void saveXY(int num);
    void import();
    void savePeriod();
    void fft();

    void onAddGraphData(int num, ...);
    void onAddGraphPoint(int num, double x, double y);

public slots:
    void closeEvent(QCloseEvent *event);
    void onMenu(PlotPopupMenu::MenuType type, bool checked);
    void onClear(int graphIndex);
    void onFullScreen(int graphIndex);
    void onSetVisible(int graphIndex, bool checked);
    void onChangeRange(RangeDialog::Range m_range);
    void onChangeUpdatePeriod(double time);

private:
    QString m_title = "";
    QCPTextElement *m_eTitle;
    PlotPopupMenu *m_menu = nullptr;
    RangeDialog *m_rangeDialog = nullptr;
    UpdateTimeDialog *m_updateTimeDialog = nullptr;
    SaveCurveDialog *m_saveCurveDialog = nullptr;

    RangeDialog::Range m_range;
    double m_scope; // temp

    QRubberBand* m_rubberBand = nullptr;
    QPoint m_rubberOrigin;

    QCustomPlot *m_cp;
    double m_index = 0;

    bool m_isPause = false;
    bool m_isScopeCurveUpdate = false;
    bool m_isScopeFollow = false;

    // 周期性更新曲线
    bool m_isPeriodUpdate = true;
    double m_updatePeriod = 1000; //1s更新一次
    QTimer *m_timerUpdate;

    // 周期性保存
    bool m_isPeriodSave = false;
    double m_savePeriod = 1000; //1s保存一次
    SaveCurveData *m_periodSaveCurveData;

    quint8 m_graphCnt = 0;

    bool m_isKeyControlPressed = false;

    QList<bool> m_graphVisibles;
};

Q_DECLARE_METATYPE(PlotXYParameter)

#endif // CURVE_H
